#include "Environment.h"

/**
 * Default constructor.
 */
Environment::Environment()
{}

/**
 * Default destructor.
 */
Environment::~Environment()
{
	this->end();
}

/**
 * Initiates the SDL graphics and creates a window.
 *
 * @param screenWidth - intvalue setting windows width.
 * @param screenHight - int value setting window hight.
 * @param windowsTitle - string value setting the window title.
 */
void Environment::init(int screenWidth, int screenHeight, const char *windowTitle)
{
	if (SDL_Init(SDL_INIT_VIDEO) != 0)
	{
		printf("Unable to initialize SDL: %s\n", SDL_GetError());
		this->end();
	}
 
	atexit(SDL_Quit);
	
	mScreen = SDL_SetVideoMode(screenWidth, screenHeight, 24, SDL_DOUBLEBUF | SDL_HWSURFACE);
	if (mScreen == NULL)
	{
		printf("Unable to set video mode: %s\n", SDL_GetError());
		this->end();
	}

	SDL_WM_SetCaption(windowTitle, "");

	this->loadScene();

	this->gameLoop();
}

/**
 * Quits the SDL.
 */
void Environment::end()
{
	SDL_Quit();
}

/**
 * Game loop that controls the games functions
 */
void Environment::gameLoop()
{
	int locX = mScreen->w / 2;
	int locY = mScreen->h / 2;

	static double lastFrameTime = 0.0;
	static double cyclesLeftOver = 0.0;
	double currentTime;
	double updateIterations;
  
	while(true)
	{
		currentTime = SDL_GetTicks() * 0.001f;
		updateIterations = ((currentTime - lastFrameTime) + cyclesLeftOver);
	  
		if (updateIterations > (MAX_CYCLES_PER_FRAME * UPDATE_INTERVAL))
		{
			updateIterations = (MAX_CYCLES_PER_FRAME * UPDATE_INTERVAL);
		}
	  
		while (updateIterations > UPDATE_INTERVAL)
		{
			updateIterations -= UPDATE_INTERVAL;
	    
			//updateGame(); /* Update game state a variable number of times */
			SDL_Event event;
			SDL_PollEvent( &event );

			if( event.type == SDL_QUIT ) break;

			if( event.type == SDL_KEYDOWN )
			{
				if( event.key.keysym.sym == SDLK_ESCAPE ) break;

				if( event.key.keysym.sym == SDLK_UP )
					locY = locY - 1;
				if( event.key.keysym.sym == SDLK_DOWN )
					locY = locY + 1;
				if( event.key.keysym.sym == SDLK_LEFT )
					locX = locX - 1;
				if( event.key.keysym.sym == SDLK_RIGHT )
					locX = locX + 1;
			}
		}
	  
		cyclesLeftOver = updateIterations;
		lastFrameTime = currentTime;
	  
		//drawScene(); /* Draw the scene only once */
		//if(this->lockFramesPerSecond(60))
			this->gameAction(locX, locY);

		SDL_Delay(16);
	}

	/*int locX = mScreen->w / 2;
	int locY = mScreen->h / 2;*/

	/*while( true )
	{
		SDL_Event event;
		SDL_PollEvent( &event );

		if( event.type == SDL_QUIT ) break;

		if( event.type == SDL_KEYDOWN )
		{
			if( event.key.keysym.sym == SDLK_ESCAPE ) break;

			if( event.key.keysym.sym == SDLK_UP )
				locY = locY - 1;
			if( event.key.keysym.sym == SDLK_DOWN )
				locY = locY + 1;
			if( event.key.keysym.sym == SDLK_LEFT )
				locX = locX - 1;
			if( event.key.keysym.sym == SDLK_RIGHT )
				locX = locX + 1;
		}
		this->gameAction(locX, locY);
		SDL_Delay(16);
	}*/
}

/**
 * Updates screen content.
 */
void Environment::gameAction(int posX, int posY)
{	
	SDL_FillRect( mScreen, &mScreen->clip_rect, SDL_MapRGB(mScreen->format, 57, 57, 57 ));

	SDL_Rect src, dest;

	src.x = 0;
	src.y = 0;
	src.w = mImage->w;
	src.h = mImage->h;
 
	dest.x = posX;
	dest.y = posY;	
	dest.w = mImage->w;
	dest.h = mImage->h;
 
	SDL_BlitSurface(mImage, &src, mScreen, &dest);

	// update the screen buffer
	SDL_Flip(mScreen);
}

/**
 * Loads a image.
 * 
 * @param string containing name of file.
 * @returns an SDL_Surface containing image.
 */
SDL_Surface* Environment::loadImage(std::string imgName)
{
	SDL_Surface *image;
	SDL_Surface *temp;
 
	
	temp = IMG_Load( imgName.c_str() );
	if ( temp == NULL )
	{
		printf( "Unable to load bitmap: %s\n", SDL_GetError() );
		this->end();
	}
	
	image = SDL_DisplayFormatAlpha(temp);
	SDL_FreeSurface(temp);

	return image;
}

/**
 * Loads resources used by game.
 */
void Environment::loadScene()
{
	mImage = this->loadImage( "ship.png" );
}
